package gl.android.biyiqing;

import gl.BaseActivity;
import gl.android.biyiqing.event.GOTOFollow;
import gl.android.biyiqing.fragment.BaseFragment;
import gl.android.biyiqing.fragment.DiscoverFragment;
import gl.android.biyiqing.fragment.HomeFragment;
import gl.android.biyiqing.fragment.MineFragment;
import gl.android.biyiqing.fragment.SearchFragment;
import gl.android.biyiqing.fragment.TalkListFragment;
import gl.common.DataLoadFail;
import gl.common.UserManger;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class BottomTabActivity extends BaseActivity implements
		View.OnClickListener, OnCheckedChangeListener, OnPageChangeListener {

	/**
	 * The {@link android.support.v4.view.PagerAdapter} that will provide
	 * fragments for each of the sections. We use a {@link FragmentPagerAdapter}
	 * derivative, which will keep every loaded fragment in memory. If this
	 * becomes too memory intensive, it may be best to switch to a
	 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
	 */
	SectionsPagerAdapter mSectionsPagerAdapter;

	/**
	 * The {@link ViewPager} that will host the section contents.
	 */
	ViewPager mViewPager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tab_buttom);
		bindViews();
	}

	private RadioGroup main_linearlayout_footer;

	// End Of Content View Elements

	private void bindViews() {

		mSectionsPagerAdapter = new SectionsPagerAdapter(
				getSupportFragmentManager());
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setOffscreenPageLimit(mSectionsPagerAdapter.getCount());
		mViewPager.setAdapter(mSectionsPagerAdapter);
		mViewPager.setOnPageChangeListener(this);
		main_linearlayout_footer = (RadioGroup) findViewById(R.id.main_linearlayout_footer);
		main_linearlayout_footer.setOnCheckedChangeListener(this);
		InitImageView();
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
		case R.id.main_footbar_home:
			mViewPager.setCurrentItem(0);
			break;
		case R.id.main_footbar_search:
			mViewPager.setCurrentItem(1);
			break;
		case R.id.main_footbar_discover:
			mViewPager.setCurrentItem(2);
			break;
		case R.id.main_footbar_talk:
			mViewPager.setCurrentItem(3);
			break;
		case R.id.main_footbar_mime:
			mViewPager.setCurrentItem(4);
			break;
		default:
			mViewPager.setCurrentItem(0);
			break;
		}

	}

	public class SectionsPagerAdapter extends FragmentPagerAdapter {

		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			switch (position) {
			case 0:
				return HomeFragment.newInstance();
			case 1:
				return SearchFragment.newInstance();
			case 2:
				return DiscoverFragment.newInstance();
			case 3:
				return TalkListFragment.newInstance();
			case 4:
				return BaseFragment.creatBaseFramgent(
						UserManger.getInstance().userid, MineFragment.class);
			default:
				return HomeFragment.newInstance();

			}

		}

		@Override
		public int getCount() {
			return 5;
		}

	}
	@Override
	public void onBackPressed() {
//		exitPressAgain();
		Intent home = new Intent(Intent.ACTION_MAIN);  
		home.addCategory(Intent.CATEGORY_HOME);   
		startActivity(home); 
	}
	private ImageView cursor;// 动画图片
	private int lastPositon = 0;// 当前页卡编号

	private int screenW;

	/**
	 * 初始化动画
	 */
	private void InitImageView() {
		cursor = (ImageView) findViewById(R.id.indr_top);
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		screenW = dm.widthPixels;// 获取分辨率宽度
		LayoutParams mLayoutParams = cursor.getLayoutParams();
		mLayoutParams.width = (screenW / mViewPager.getAdapter().getCount());
		cursor.setLayoutParams(mLayoutParams);// 设置动画初始位置
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int arg0) {
		((RadioButton) main_linearlayout_footer.getChildAt(arg0))
				.setChecked(true);

		Animation animation = null;
		animation = new TranslateAnimation(lastPositon
				* (screenW / mViewPager.getAdapter().getCount()), arg0
				* (screenW / mViewPager.getAdapter().getCount()), 0, 0);
		lastPositon = arg0;
		animation.setFillAfter(true);
		animation.setDuration(300);
		cursor.startAnimation(animation);
	}
	public void onEvent(GOTOFollow GOTOFollow) {
		mViewPager.setCurrentItem(0);
	}
}
